package com.example.visitedsys.impl;

import com.example.visitedsys.converter.MenuConverter;
import com.example.visitedsys.informationGet.PermissionNodeVO;
import com.example.visitedsys.informationGet.PermissionVO;
import com.example.visitedsys.mapper.PermissionMapper;
import com.example.visitedsys.pojo.Permission;
import com.example.visitedsys.service.PermissionService;
import com.example.visitedsys.utils.MenuTreeBuilder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * 〈权限管理〉<br>
 * 〈〉
 * @author  SongPeng
 * @date  2021/11/01 14:25
 * @version 1.0
 */

@Service
public class PermissionServiceImpl implements PermissionService {

    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public List<Permission> findPermissionsByRole(Long roleId) {
        return permissionMapper.findPermissionsByRole(roleId);
    }

    /**
     * 加载菜单树（按钮和菜单）
     *
     * @return
     */
    @Override
    public List<PermissionNodeVO> findMenuTree() {
        List<Permission> permissions = permissionMapper.selectAll();
        List<PermissionNodeVO> PermissionNodeVOS = MenuConverter.converterToALLMenuNodeVO(permissions);
        return MenuTreeBuilder.build(PermissionNodeVOS);
    }

    @Override
    public List<Long> findOpenIds() {
        List<Long> ids=new ArrayList<>();
        List<Permission> menus = permissionMapper.selectAll();
        if(!CollectionUtils.isEmpty(menus)){
            for (Permission permission : menus) {
                if(permission.getOpen()==1){
                    ids.add(permission.getPermissionId());
                }
            }
        }
        return ids;
    }

    @Override
    public PermissionVO edit(Long id) {
        return null;
    }

}
